package com.galgram.project.controller;

import java.time.LocalDateTime;
import java.util.List;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.galgram.common.annotation.Log;
import com.galgram.common.core.controller.BaseController;
import com.galgram.common.core.domain.AjaxResult;
import com.galgram.common.enums.BusinessType;
import com.galgram.project.domain.FxMessage;
import com.galgram.project.service.IFxMessageService;
import com.galgram.common.utils.poi.ExcelUtil;
import com.galgram.common.core.page.TableDataInfo;

/**
 * 消息Controller
 *
 * @author galgram
 * @date 2024-11-20
 */
@RestController
@RequestMapping("/project/message")
public class FxMessageController extends BaseController {
    @Autowired
    private IFxMessageService fxMessageService;

    /**
     * 查询消息列表
     */
    @GetMapping("/getList")
    public TableDataInfo getMessageList(FxMessage fxMessage) {
        startPage();
        List<FxMessage> list = fxMessageService.getMessageList(fxMessage);
        return getDataTable(list);
    }

    /**
     * 查询消息列表
     */
    @PreAuthorize("@ss.hasPermi('project:message:list')")
    @GetMapping("/list")
    public TableDataInfo list(FxMessage fxMessage) {
        startPage();
        List<FxMessage> list = fxMessageService.selectFxMessageList(fxMessage);
        return getDataTable(list);
    }

    /**
     * 获取消息详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:message:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fxMessageService.getById(id));
    }

    /**
     * 新增消息
     */
    @PreAuthorize("@ss.hasPermi('project:message:add')")
    @Log(title = "消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FxMessage fxMessage) {
        fxMessage.setDate(LocalDateTime.now());
        fxMessageService.save(fxMessage);
        return success("添加成功");
    }

    /**
     * 修改消息
     */
    @PreAuthorize("@ss.hasPermi('project:message:edit')")
    @Log(title = "消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FxMessage fxMessage) {
        fxMessageService.updateById(fxMessage);
        return success("修改成功");
    }

    /**
     * 删除消息
     */
    @PreAuthorize("@ss.hasPermi('project:message:remove')")
    @Log(title = "消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        fxMessageService.removeByIds(ids);
        return success("删除成功");
    }


    /**
     * 导出消息列表
     */
    @PreAuthorize("@ss.hasPermi('project:message:export')")
    @Log(title = "消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FxMessage fxMessage) {
        List<FxMessage> list = fxMessageService.selectFxMessageList(fxMessage);
        ExcelUtil<FxMessage> util = new ExcelUtil<FxMessage>(FxMessage.class);
        util.exportExcel(response, list, "消息数据");
    }

    /**
     * 导入消息列表
     */
    @PreAuthorize("@ss.hasPermi('project:message:import')")
    @Log(title = "消息", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<FxMessage> util = new ExcelUtil<FxMessage>(FxMessage.class);
        List<FxMessage> userList = util.importExcel(file.getInputStream());
        fxMessageService.saveBatch(userList);
        return AjaxResult.success("导入成功");
    }

    /**
     * 下载模版
     */
    @PreAuthorize("@ss.hasPermi('project:message:export')")
    @PostMapping("/importTemplate")
    public void exportTemplate(HttpServletResponse response) {
        List<FxMessage> list = new ArrayList<FxMessage>();
        ExcelUtil<FxMessage> util = new ExcelUtil<FxMessage>(FxMessage.class);
        util.exportExcel(response, list, "消息数据");
    }
}
